import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useTaskStore = defineStore('task', () => {
  // 所有任务列表
  const tasks = ref([
    {
      id: 1,
      title: '上海大学黄浦校区科技代课',
      content: '科技楼三号楼4023教室线性代数一节要签到',
      author: '乐小乐',
      time: '06-18 15:10',
      avatar: 'https://picsum.photos/40/40?random=1',
      images: [],
      phone: '159522422687',
      owner: true // 是否本人发布
    },
    {
      id: 2,
      title: '图书馆占座',
      content: '需要明天上午9点到下午5点在图书馆占座',
      author: '陈晨',
      time: '06-17 8:45',
      avatar: 'https://picsum.photos/40/40?random=2',
      images: [],
      phone: '139522472685',
      owner: true // 是否本人发布
    },
    {
      id: 3,
      title: '宿舍楼送餐',
      content: '需要上午12点到下午2点在宿舍楼下送餐',
      author: '刘大宇',
      time: '06-18 8:45',
      avatar: 'https://picsum.photos/40/40?random=3',
      images: [],
      phone: '110521241234',
      owner: true // 是否本人发布
    },
    {
      id: 4,
      title: '一起跑步',
      content: '需要晚上6点后操场跑步',
      author: '曾小庆',
      time: '06-18 8:45',
      avatar: 'https://picsum.photos/40/40?random=4',
      images: [],
      phone: '135521241234',
      owner: true // 是否本人发布
    },
    {
      id: 5,
      title: '帮拿快递',
      content: '需要下午下课后帮我拿快递到17栋',
      author: '周凯皇',
      time: '06-18 17:45',
      avatar: 'https://picsum.photos/40/40?random=5',
      images: [],
      phone: '130521241234',
      owner: true // 是否本人发布
    }
  ])
  // 新增任务
  function addTask(task) {
    tasks.value.unshift(task)
  }
  // 关键词搜索
  function search(keyword) {
    if (!keyword) return tasks.value
    return tasks.value.filter(t => t.title.includes(keyword) || t.content.includes(keyword) || t.author.includes(keyword))
  }
  // 获取我发布的任务
  const myTasks = computed(() => tasks.value.filter(t => t.owner))
  return { tasks, addTask, search, myTasks }
}) 